package com.hckjsec.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.system.domain.*;
import com.hckjsec.system.service.ISysDispatchService;
import com.hckjsec.system.service.ISysNoticeUserService;
import com.hckjsec.system.service.ISysPartyBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.core.page.TableDataInfo;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.system.service.ISysNoticeService;

/**
 * 公告 信息操作处理
 * 
 * @author cyt
 */
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController
{
    @Autowired
    private ISysNoticeService noticeService;

    @Autowired
    private ISysNoticeUserService sysNoticeUserService;

    @Autowired
    private ISysPartyBuildingService sysPartyBuildingService;

    @Autowired
    private ISysDispatchService sysDispatchService;

    /**
     * 获取通知公告列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysNotice notice)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            notice.setDeptId(SecurityUtils.getDeptId());
        }
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable Long noticeId)
    {
        return AjaxResult.success(noticeService.selectNoticeById(noticeId));
    }

    /**
     * 新增通知公告
     */
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNotice notice)
    {
        notice.setDeptId(SecurityUtils.getDeptId());
        notice.setCreateBy(SecurityUtils.getUsername());
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 修改通知公告
     */
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNotice notice)
    {
        notice.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除通知公告
     */
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds)
    {
        return toAjax(noticeService.deleteNoticeByIds(noticeIds));
    }


    /**
     * 查询当前登录人员未读通知数量
     */
    @GetMapping("/getNoticeNum")
    public AjaxResult getNoticeNum()
    {
        Long deptId = SecurityUtils.getDeptId();
        Long userId = SecurityUtils.getUserId();
        SysNotice sysNotice = new SysNotice();
        sysNotice.setDeptId(deptId);
        //通知公告
        List<SysNotice> list = noticeService.selectNoticeList(sysNotice);
        list.forEach(kv->{
            SysNoticeUser sysNoticeUser = new SysNoticeUser();
            sysNoticeUser.setUserId(userId);
            sysNoticeUser.setNoticeId(kv.getNoticeId());
            sysNoticeUser.setType("notice");
            List<SysNoticeUser> sysNoticeUserList = sysNoticeUserService.selectSysNoticeUserList(sysNoticeUser);
            if (StringUtils.isEmpty(sysNoticeUserList) ){
                sysNoticeUser.setState("0");
                sysNoticeUser.setType("notice");
                sysNoticeUserService.insertSysNoticeUser(sysNoticeUser);
            }
        });
        //党建管理
        SysPartyBuilding   sysPartyBuilding = new SysPartyBuilding();
        sysPartyBuilding.setOrganization(deptId);
        sysPartyBuilding.setState("true");
        List<SysPartyBuilding> sysPartyBuildingList = sysPartyBuildingService.selectSysPartyBuildingList(sysPartyBuilding);
        sysPartyBuildingList.forEach(kv->{
            SysNoticeUser sysNoticeUser = new SysNoticeUser();
            sysNoticeUser.setUserId(userId);
            sysNoticeUser.setNoticeId(kv.getPartyId());
            sysNoticeUser.setType("party");
            List<SysNoticeUser> sysNoticeUserList = sysNoticeUserService.selectSysNoticeUserList(sysNoticeUser);
            if (StringUtils.isEmpty(sysNoticeUserList) ){
                sysNoticeUser.setState("0");
                sysNoticeUser.setType("party");
                sysNoticeUserService.insertSysNoticeUser(sysNoticeUser);
            }
        });
        //发文
        SysDispatch sysDispatch = new SysDispatch();
        sysDispatch.setOrganization(deptId);
        List<SysDispatch> sysDispatchList = sysDispatchService.selectSysDispatchList(sysDispatch);
        sysDispatchList.forEach(kv->{
            SysNoticeUser sysNoticeUser = new SysNoticeUser();
            sysNoticeUser.setUserId(userId);
            sysNoticeUser.setNoticeId(kv.getDispatchId());
            sysNoticeUser.setType("dispatch");
            List<SysNoticeUser> sysNoticeUserList = sysNoticeUserService.selectSysNoticeUserList(sysNoticeUser);
            if (StringUtils.isEmpty(sysNoticeUserList) ){
                sysNoticeUser.setState("0");
                sysNoticeUser.setType("dispatch");
                sysNoticeUserService.insertSysNoticeUser(sysNoticeUser);
            }
        });
        SysNoticeUser sysNoticeUser = new SysNoticeUser();
        sysNoticeUser.setUserId(userId);
        sysNoticeUser.setState("0");
        List<SysNoticeUser> sysNoticeUserList = sysNoticeUserService.selectSysNoticeUserList(sysNoticeUser);
        return AjaxResult.success(sysNoticeUserList.size());
    }


    /**
     * 获取未读、已读详情
     */
    @GetMapping("/getNoticeList")
    public AjaxResult getNoticeList()
    {
        SysNoticeData sysNoticeData = new SysNoticeData();
        Long deptId = SecurityUtils.getDeptId();
        Long userId = SecurityUtils.getUserId();
        SysNotice sysNotice = new SysNotice();
        sysNotice.setDeptId(deptId);
        //通知公告
        List<SysNotice> list = noticeService.selectNoticeList(sysNotice);
        list.forEach(kv->{
            SysNoticeUser sysNoticeUser = new SysNoticeUser();
            sysNoticeUser.setUserId(userId);
            sysNoticeUser.setNoticeId(kv.getNoticeId());
            sysNoticeUser.setType("notice");
            List<SysNoticeUser> sysNoticeUserList = sysNoticeUserService.selectSysNoticeUserList(sysNoticeUser);
            if (StringUtils.isEmpty(sysNoticeUserList) ){
                sysNoticeUser.setState("0");
                sysNoticeUser.setType("notice");
                sysNoticeUserService.insertSysNoticeUser(sysNoticeUser);
            }
        });
        //党建管理
        SysPartyBuilding   sysPartyBuilding = new SysPartyBuilding();
        sysPartyBuilding.setOrganization(deptId);
        sysPartyBuilding.setState("true");
        List<SysPartyBuilding> sysPartyBuildingList = sysPartyBuildingService.selectSysPartyBuildingList(sysPartyBuilding);
        sysPartyBuildingList.forEach(kv->{
            SysNoticeUser sysNoticeUser = new SysNoticeUser();
            sysNoticeUser.setUserId(userId);
            sysNoticeUser.setNoticeId(kv.getPartyId());
            sysNoticeUser.setType("party");
            List<SysNoticeUser> sysNoticeUserList = sysNoticeUserService.selectSysNoticeUserList(sysNoticeUser);
            if (StringUtils.isEmpty(sysNoticeUserList) ){
                sysNoticeUser.setState("0");
                sysNoticeUser.setType("party");
                sysNoticeUserService.insertSysNoticeUser(sysNoticeUser);
            }
        });
        //发文
        SysDispatch sysDispatch = new SysDispatch();
        sysDispatch.setOrganization(deptId);
        List<SysDispatch> sysDispatchList = sysDispatchService.selectSysDispatchList(sysDispatch);
        sysDispatchList.forEach(kv->{
            SysNoticeUser sysNoticeUser = new SysNoticeUser();
            sysNoticeUser.setUserId(userId);
            sysNoticeUser.setNoticeId(kv.getDispatchId());
            sysNoticeUser.setType("dispatch");
            List<SysNoticeUser> sysNoticeUserList = sysNoticeUserService.selectSysNoticeUserList(sysNoticeUser);
            if (StringUtils.isEmpty(sysNoticeUserList) ){
                sysNoticeUser.setState("0");
                sysNoticeUser.setType("dispatch");
                sysNoticeUserService.insertSysNoticeUser(sysNoticeUser);
            }
        });

        List<NoticeView> unreadList = new ArrayList<>();
        List<NoticeView> readList = new ArrayList<>();
        list.forEach(kv->{
            NoticeView noticeView = new NoticeView();
            noticeView.setNoticeId(kv.getNoticeId());
            noticeView.setNoticeType("notice");
            noticeView.setNoticeBelonging("通知公告");
            noticeView.setNoticeTitle(kv.getNoticeTitle());
            noticeView.setNoticeContent(kv.getNoticeContent());
            noticeView.setCreateTime(kv.getCreateTime());
            SysNoticeUser sysNoticeUser = new SysNoticeUser();
            sysNoticeUser.setUserId(userId);
            sysNoticeUser.setNoticeId(kv.getNoticeId());
            sysNoticeUser.setType("notice");
            List<SysNoticeUser> sysNoticeUserList = sysNoticeUserService.selectSysNoticeUserList(sysNoticeUser);
            if (StringUtils.isEmpty(sysNoticeUserList) ){
                unreadList.add(noticeView);
            }else{
                String state = sysNoticeUserList.get(0).getState();
                if ("0".equals(state)){
                    unreadList.add(noticeView);
                }else if ("1".equals(state)){
                    readList.add(noticeView);
                }
            }
        });
        sysPartyBuildingList.forEach(kv->{
            NoticeView noticeView = new NoticeView();
            noticeView.setNoticeId(kv.getPartyId());
            noticeView.setNoticeType("party");
            noticeView.setNoticeBelonging("党建管理");
            noticeView.setNoticeTitle(kv.getActivityName());
            noticeView.setNoticeContent(kv.getContent());
            noticeView.setCreateTime(kv.getOperatortime());
            SysNoticeUser sysNoticeUser = new SysNoticeUser();
            sysNoticeUser.setUserId(userId);
            sysNoticeUser.setNoticeId(kv.getPartyId());
            sysNoticeUser.setType("party");
            List<SysNoticeUser> sysNoticeUserList = sysNoticeUserService.selectSysNoticeUserList(sysNoticeUser);
            if (StringUtils.isEmpty(sysNoticeUserList) ){
                unreadList.add(noticeView);
            }else{
                String state = sysNoticeUserList.get(0).getState();
                if ("0".equals(state)){
                    unreadList.add(noticeView);
                }else if ("1".equals(state)){
                    readList.add(noticeView);
                }
            }
        });
        sysDispatchList.forEach(kv->{
            NoticeView noticeView = new NoticeView();
            noticeView.setNoticeId(kv.getDispatchId());
            noticeView.setNoticeType("dispatch");
            noticeView.setNoticeBelonging("发文");
            noticeView.setNoticeTitle(kv.getTitle());
            noticeView.setNoticeContent(kv.getContent());
            noticeView.setCreateTime(kv.getOperatortime());
            SysNoticeUser sysNoticeUser = new SysNoticeUser();
            sysNoticeUser.setUserId(userId);
            sysNoticeUser.setNoticeId(kv.getDispatchId());
            sysNoticeUser.setType("dispatch");
            List<SysNoticeUser> sysNoticeUserList = sysNoticeUserService.selectSysNoticeUserList(sysNoticeUser);
            if (StringUtils.isEmpty(sysNoticeUserList) ){
                unreadList.add(noticeView);
            }else{
                String state = sysNoticeUserList.get(0).getState();
                if ("0".equals(state)){
                    unreadList.add(noticeView);
                }else if ("1".equals(state)){
                    readList.add(noticeView);
                }
            }
        });
        sysNoticeData.setUnreadList(unreadList);
        sysNoticeData.setReadList(readList);
        return AjaxResult.success(sysNoticeData);
    }


    /**
     * 获取详情
     */
    @PostMapping("/getNoticeDetails")
    public AjaxResult getNoticeDetails(@RequestBody NoticeView noticeView)
    {
        String type = StringUtils.isHcNull(noticeView.getNoticeType(),"");
        NoticeView noticeView1 = new NoticeView();
        if ("notice".equals(type)){
            SysNotice sysNotice =  noticeService.selectNoticeById(noticeView.getNoticeId());
            noticeView1.setNoticeId(sysNotice.getNoticeId());
            noticeView1.setNoticeType("notice");
            noticeView1.setNoticeBelonging("通知公告");
            noticeView1.setNoticeTitle(sysNotice.getNoticeTitle());
            noticeView1.setNoticeContent(sysNotice.getNoticeContent());
            noticeView1.setCreateTime(sysNotice.getCreateTime());
        }else if("party".equals(type)){
            SysPartyBuilding sysPartyBuilding = sysPartyBuildingService.selectSysPartyBuildingById(noticeView.getNoticeId());
            noticeView1.setNoticeId(sysPartyBuilding.getPartyId());
            noticeView1.setNoticeType("party");
            noticeView1.setNoticeBelonging("党建管理");
            noticeView1.setNoticeTitle(sysPartyBuilding.getActivityName());
            noticeView1.setNoticeContent(sysPartyBuilding.getContent());
            noticeView1.setCreateTime(sysPartyBuilding.getOperatortime());
        }else if("party".equals(type)){
            SysDispatch  sysDispatch = sysDispatchService.selectSysDispatchById(noticeView.getNoticeId());
            noticeView1.setNoticeId(sysDispatch.getDispatchId());
            noticeView1.setNoticeType("dispatch");
            noticeView1.setNoticeBelonging("发文");
            noticeView1.setNoticeTitle(sysDispatch.getTitle());
            noticeView1.setNoticeContent(sysDispatch.getContent());
            noticeView1.setCreateTime(sysDispatch.getOperatortime());
        }
        return AjaxResult.success(noticeView1);
    }


    /**
     * 修改状态
     */
    @PutMapping("/updateNoticeList")
    public AjaxResult updateNoticeList(@RequestBody NoticeView noticeView)
    {
        Long deptId = SecurityUtils.getDeptId();
        Long userId = SecurityUtils.getUserId();
        SysNoticeUser noticeUser = new SysNoticeUser();
        noticeUser.setUserId(userId);
        noticeUser.setNoticeId(noticeView.getNoticeId());
        noticeUser.setType(noticeView.getNoticeType());
        List<SysNoticeUser> sysNoticeUserList = sysNoticeUserService.selectSysNoticeUserList(noticeUser);
        sysNoticeUserList.forEach(kv->{
            kv.setState(noticeView.getState());
            sysNoticeUserService.updateSysNoticeUser(kv);
        });
        return AjaxResult.success();
    }


    /**
     * 全部标记为已读
     */
    @PutMapping("/updateReadNoticeList")
    public AjaxResult updateNoticeList()
    {
        Long deptId = SecurityUtils.getDeptId();
        Long userId = SecurityUtils.getUserId();
        SysNoticeUser noticeUser = new SysNoticeUser();
        noticeUser.setUserId(userId);
        noticeUser.setState("0");
        List<SysNoticeUser> sysNoticeUserList = sysNoticeUserService.selectSysNoticeUserList(noticeUser);
        sysNoticeUserList.forEach(kv->{
            kv.setState("1");
            sysNoticeUserService.updateSysNoticeUser(kv);
        });
        return AjaxResult.success();
    }


    /**
     * 全部标记为删除
     */
    @PutMapping("/deleteNoticeList")
    public AjaxResult deleteNoticeList()
    {
        Long deptId = SecurityUtils.getDeptId();
        Long userId = SecurityUtils.getUserId();
        SysNoticeUser noticeUser = new SysNoticeUser();
        noticeUser.setUserId(userId);
        noticeUser.setState("1");
        List<SysNoticeUser> sysNoticeUserList = sysNoticeUserService.selectSysNoticeUserList(noticeUser);
        sysNoticeUserList.forEach(kv->{
            kv.setState("2");
            sysNoticeUserService.updateSysNoticeUser(kv);
        });
        return AjaxResult.success();
    }
}
